/**
 * Created by JetBrains WebStorm.
 * User: Silence
 * Date: 3/31/12
 * Time: 1:50 AM
 * To change this template use File | Settings | File Templates.
 */

Ext.require([
    'Ext.data.*',
    'Ext.grid.*',
    'Ext.tree.*'
]);

var sm = Ext.create('Ext.selection.RowModel', {
    listeners: {
        selectionchange: function(sm, selections) {
            for (var i = 0; i < selections.length; i++) {
                var record = selections[i];
            }
            Ext.getCmp('removesourcesbutton').setDisabled(selections.length == 0);
        }
    }
});

Ext.define('EM.view.event.SourcePanel', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.sourcepanel',
    store: 'SourceStore',
    bbar: [
        {
            id: 'addsourcebutton',
            iconCls: 'icon-add',
            tooltip:'Add new source',
            text: 'Add Source'
        }, '-',
        {
            id: 'removesourcesbutton',
            text:'Remove Source',
            tooltip:'Remove source',
            iconCls:'remove'
        }
    ],
    selModel: sm,
    plugins: [
        Ext.create('Ext.grid.plugin.CellEditing', {
            clicksToEdit: 2
            /*,
            listeners: {
                edit: function(editor, e) {
                    var temp = Ext.getCmp('budgettreepanel').getStore().getNodeById(e.record.get('id'));
                    if (e.field == 'cost' || e.field == 'budget') {
                        if (temp.get('pid') != 0) {
                            var deltaValue = e.value - e.originalValue;
                            do {
                                temp = Ext.getCmp('budgettreepanel').getStore().getNodeById(temp.get('pid'));
                                temp.set(e.field, temp.get(e.field) + deltaValue);
                            } while  (temp.get('pid') != 0);
                        }
                    }
                    else {
                    }
                    Ext.getCmp('budgettreepanel').getStore().sync();
                },
                beforeedit: function(e, editor){
                    if (e.record.get('leaf') || (e.field != 'cost' && e.field != 'budget')) {
                        return true;
                    }
                    else return false;
                }
            }*/
        })
    ],
    columns: [
        {
            xtype: 'gridcolumn',
            dataIndex: 'name',
            text: 'Name',
            editor: {xtype: 'textfield', allowBlank: false}
        },
        {
            xtype: 'gridcolumn',
            dataIndex: 'description',
            text: 'Description',
            editor: {xtype: 'textfield'}
        },
        {
            //we must use the templateheader component so we can use a custom tpl
            xtype: 'templatecolumn',
            text: 'Cost',
            flex: 1,
            sortable: true,
            dataIndex: 'cost',
            align: 'center',
            editor: {xtype: 'textfield'},
            //add in the custom tpl for the rows
            tpl: Ext.create('Ext.XTemplate', '{cost:this.formatHours}', {
                formatHours: function (v) {
                    return  (v < 0? '-': '') + '$' + Number(v < 0? -v: v).toFixed(2);
                }
            })
        }
    ],
    updateStore: function(itemId, sourceId) {
        this.store.clearFilter(true);
        this.store.filter('pid', itemId);
        this.store.each(function(record, idx) {
            if (record.get('id') == sourceId) {
                Ext.getCmp('itemsources').getSelectionModel().select(idx);
            }
        });
    }
});